import React, { Component } from 'react'
import store from '../../redux/store'
import {creatJia,creatJian,creatYiJia} from '../../redux/count_action'
export default class index extends Component {
  componentDidMount(){
    store.subscribe(()=>{
      this.setState({})
    })

  }
  jia = () =>{
   store.dispatch(creatJia(this.seValue.value * 1))
  }
  jian = () =>{
    store.dispatch(creatJian(this.seValue.value * 1))
  }
  jiJia = () =>{
    const count = store.getState()
    if(count%2 !== 0){
      store.dispatch(creatJia(this.seValue.value * 1))
    }
  }
  yiJian = () =>{
    setTimeout(()=>{
      store.dispatch(creatYiJia(this.seValue.value * 1,500))
    },2000)
  }
  render() {
    return (
      <div>
        <h2>当前的和为：{store.getState()}</h2>
        <select name="" id="" ref={(c)=>{this.seValue = c}} >
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>&nbsp;
        <button onClick={this.jia}>+</button>&nbsp;
        <button onClick={this.jian}>-</button>&nbsp;
        <button onClick={this.jiJia}>当前和为奇数则加</button>&nbsp;
        <button onClick={this.yiJian}>异步加</button>&nbsp;
      </div>
    )
  }
}
